﻿using System.Collections.Generic;
using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Content.Spritesheet.Player;
using Super_Management_Mayhem.Control;
using Super_Management_Mayhem.GameRoot;
using Super_Management_Mayhem.Screens.Playing.GameMode;
using Super_Management_Mayhem.Screens.Playing.Maps;
using Super_Management_Mayhem.Screens.Playing.Players;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Rendering;

namespace Super_Management_Mayhem.Screens.Playing
{
    public sealed class Playscreen : Container, IStateChanger
    {
        public delegate void OnFadeToBlackHandler();

        public event OnFadeToBlackHandler OnFadeToBlack;

        public delegate void OnEndGameHandler(int[] scores);

        public event OnEndGameHandler OnEndGame;

        public const bool SHOW_COLLISION = false;

        public enum MapType
        {
            Castle
        }

        public static PlayerContainer CurrentlyDrawnPlayer { get; private set; }
        

        public static BaseMap Map { get; private set; }

        public static IGameMode Controller;

        public Playscreen(MapType type,
                          GameMode.GameMode.GameModes mode,
                          int playerAmount,
                          List<PlayerDefinition> players)
        {

            BorderSheet.LoadContent();

            ManagerSheet.LoadContent();
            NativeSheet.LoadContent();

            ItemSheet.LoadContent();
            UISheet.LoadContent();
            PointSheet.LoadContent();
            ParticleSheet.LoadContent();
            
            switch(mode)
            {
                case GameMode.GameMode.GameModes.CaptureTheFlag:
                    break;
                case GameMode.GameMode.GameModes.DeathMatch:
                    break;
                case GameMode.GameMode.GameModes.TeamDeathMatch:
                    break;
                case GameMode.GameMode.GameModes.InvadedDungeon:
                    break;
                case GameMode.GameMode.GameModes.MoneyMayhem:
                case GameMode.GameMode.GameModes.TeamMoneyMayhem:
                    Controller = new MoneyMayhem(this);
                    break;
            }
            AddChildren((Container) Controller);

            switch (type)
            {
                case MapType.Castle:
                    Map = new CastleMap();
                    break;
            }

            for (int i = 0; i < playerAmount; i++)
            {
                Map.AddPlayer(players[i], SuperManagementMayhem.HudPosition[i], SuperManagementMayhem.HudSize);
            }

            AddChildren(Map);
        }

        public override void Draw()
        {
            for (int i = 0; i < Map.Players.Count; i++)
            {
                ViewHandler.SetView("viewPlayer" + (i+1));
                CurrentlyDrawnPlayer = Map.Players[i];
                ViewHandler.MoveView("viewPlayer" + (i+1), Map.Players[i].CameraPosition);
                base.Draw();
            }
            ViewHandler.RevertToDefaultView();
            foreach (var player in Map.Players)
            {
                player.DrawHud();
            }
        }

        public void StartGame()
        {
        }

        public void EndGame()
        {
            if (Controller.HasTimeLimit)
            {
                Map.TimesUp();
            }

            if (OnFadeToBlack != null)
                OnFadeToBlack();

            CallMethod(3000, ShowResultScreen);
        }

        void ShowResultScreen()
        {
            if (OnEndGame != null)
                OnEndGame(Controller.GetScores());
        }
    }
}
